//
//  DCIMAddFriendViewController.m
//  xmppBase
//
//  Created by striby on 16/1/26.
//  Copyright © 2016年 com.dc. All rights reserved.
//

#import "DCIMAddFriendViewController.h"

@interface DCIMAddFriendViewController ()
{
  
}

@property (strong, nonatomic) NSMutableArray *searchResult;

@end

@implementation DCIMAddFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.searchBarView = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, DCDeviceWidth, 40)];
    self.searchBarView.delegate=self;
    self.searchBarView.placeholder = @"搜索";
    [self.view addSubview:self.searchBarView];
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.searchBarView.frame.size.height+self.searchBarView.frame.origin.y, DCDeviceWidth, DCDeviceHeight)];
    self.tableView.delegate=self;
    self.tableView.dataSource =self;
    
//    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [self.view addSubview:self.tableView];
    
    
    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, TabBarHeight, 0)];
    
    [self.tableView setShowsVerticalScrollIndicator:NO];
    
    
    [[DCXMPPTool shareInstance].xmppvCardTempModule addDelegate:self delegateQueue:dispatch_get_main_queue()];
}

-(void)dealloc
{
    [[DCXMPPTool shareInstance].xmppvCardTempModule removeDelegate:self delegateQueue:dispatch_get_main_queue()];
}

#pragma mark - tableview delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.searchBarView resignFirstResponder];
     XMPPvCardTemp * card = _searchResult[indexPath.row];
 
    NSString * str = [NSString stringWithFormat:@"%@@%@",self.searchBarView.text,DomainName];
    
    DCIMUserInfoViewController * vc = [[DCIMUserInfoViewController alloc]init];
    vc.userCard = card;
    vc.userJid = [XMPPJID jidWithString:str];
    [self.navigationController pushViewController:vc animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _searchResult.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DCMyFriendTableViewCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"DCMyFriendTableViewCell" owner:nil options:nil]lastObject];
    cell.indexPath = indexPath;
    cell.delegate=self;

    [cell setHidenSelectStatusBtn:YES];
  
    XMPPvCardTemp * card = _searchResult[indexPath.row];
    
    
    
    if (card.photo) {
        cell.UserIcon.image = [UIImage imageWithData:card.photo];
    }
    else
    {
        [cell.UserIcon setImage:[UIImage imageNamed:@"defaultUserIcon"]];
    }
    
    
    
    cell.UserName.text = card.nickname;
    
    [cell ChangeCurrentBtnImage];
    
    return cell;

}

-(void)friendCellSelectEventWithIndexPath:(NSIndexPath *)indexPath isSelected:(BOOL)isSelected
{
    
}

#pragma mark 查找代理
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    return [searchBar resignFirstResponder];
}


-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    if(_searchResult==nil)
    {
        _searchResult = [[NSMutableArray alloc]initWithCapacity:1];
    }
    
    
    [_searchResult removeAllObjects];

   

    if ([searchText length]) {
        
        NSString * jidStr = [NSString stringWithFormat:@"%@@%@",searchText,DomainName];
        
        XMPPJID * jid = [XMPPJID jidWithString:jidStr];
        
        XMPPvCardTemp * card = [[DCXMPPTool shareInstance].xmppvCardTempModule vCardTempForJID:jid shouldFetch:YES];
        
        if (card!=nil) {
            if (card.nickname!=nil) {
                [_searchResult addObject:card];
            }
            
           
        }
        
        
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //reload
        [self.tableView reloadData];
    });

    
}

-(void)xmppvCardTempModule:(XMPPvCardTempModule *)vCardTempModule didReceivevCardTemp:(XMPPvCardTemp *)vCardTemp forJID:(XMPPJID *)jid
{
    if (vCardTemp!=nil) {
        if (vCardTemp.nickname!=nil) {
            [_searchResult addObject:vCardTemp];
        }
        
       
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //reload
        [self.tableView reloadData];
    });
}

@end
